package com.lingkang.sales.config.auth;

import com.lingkang.sales.service.AuthService;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

/**
 * @author linke
 * @date 2020/11/14 20:41
 * @description 权限控制
 */
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {

  @Autowired
  private AuthService authService;

  /**
   *
   * @param authentication
   * @param targetDomainObject
   * @param permission permission格式：ROLE_ADMIN:r、ROLE_USER:w ,r/w表示：读权限/写权限
   * @return
   */
  @Override
  public boolean hasPermission(Authentication authentication, Object targetDomainObject,
      Object permission) {
    User user = (User) authentication.getPrincipal();
    System.out.println(authentication.getAuthorities());
    System.out.println(user);
    boolean has=authService.hasPermission(targetDomainObject.toString(), permission.toString());
    return has;
  }

  @Override
  public boolean hasPermission(Authentication authentication, Serializable targetId,
      String targetType, Object permission) {
    return false;
  }
}
